package io.github.voishion.redismq.example.controller;

import io.github.voishion.redismq.core.RedisDelayMQSender;
import io.github.voishion.redismq.example.constant.RedisMQConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author lilu
 */
@Log4j2
@RestController
@RequestMapping(value = "/redis/delay/mq")
@RequiredArgsConstructor
public class RedisDelayMQController {

    private final RedisDelayMQSender redisDelayMQSender;

    @GetMapping("/send/{message}/{delayTime}")
    public String send(@PathVariable String message, @PathVariable Long delayTime) {
        redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE, message, delayTime, TimeUnit.SECONDS);
        return "1";
    }

    @GetMapping("/send1/{message}")
    public String send1(@PathVariable String message) {
        redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE, message, 5, TimeUnit.SECONDS);
        return "1";
    }

    @GetMapping("/send11/{message}")
    public String send11(@PathVariable String message) {
        redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE, message, 5, TimeUnit.SECONDS, 2, TimeUnit.SECONDS);
        return "1";
    }

    @GetMapping("/send111/{message}")
    public String send111(@PathVariable String message) {
        redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE, message, 5, TimeUnit.SECONDS, 3, 2, TimeUnit.SECONDS);
        return "1";
    }

    @GetMapping("/send2/{message}")
    public String send2(@PathVariable String message) {
        redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE_2, message, 5, TimeUnit.SECONDS);
        return "1";
    }

    @GetMapping("/send3")
    public String send3() {
        for (int i = 0; i < 50000; i++) {
            if (i % 2 == 0) {
                redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE, String.valueOf(i), 5, TimeUnit.SECONDS);
            } else {
                redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE_2, String.valueOf(i), 5, TimeUnit.SECONDS);
            }
            if (i == 25000) {
                System.out.println(i);
            }
        }
        return "";
    }

    @GetMapping("/send4")
    public String send4() {
        for (int i = 0; i < 10; i++) {
            redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE_2, String.valueOf(i), 5, TimeUnit.SECONDS);
        }
        return "";
    }

    @GetMapping("/send5")
    public String send5() {
        for (int i = 0; i < 100000; i++) {
            redisDelayMQSender.send(RedisMQConstant.TEST_DELAY_QUEUE_2, String.valueOf(i), 5, TimeUnit.SECONDS);
        }
        return "";
    }

}
